/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.application.configuration;

import com.jb.framework.PersistentDescriptor;
import com.jb.framework.PersistentManagerRegistry;
import com.jb.framework.database.DatabaseManager;
import com.jb.util.ClassUtil;
import com.jb.web.model.ApplicationConfig;


/**
 * This class creates all database entities needed for the
 * application. Run this file would remove all the data in the database and
 * recreate the schema. Be careful!
 *
 * @author Tim Wang
 * @version 1.0
 */
public class CreateSchema {
    public void create() {
        createTable(ApplicationConfig.class, false);
    }

    public boolean createTable(String className, boolean drop) {
        Class clazz = ClassUtil.getClass(className);
        if(clazz!=null)
        {
            createTable(clazz, drop);
            return true;
        }
        return false;
    }

    public boolean createTable(Class clazz, boolean drop) {
        PersistentDescriptor dbDescriptor = PersistentManagerRegistry.getPersistentDescriptor(clazz);
        DatabaseManager pm = (DatabaseManager)PersistentManagerRegistry.getPersistentManager(clazz);
        pm.registerDescriptor(clazz, dbDescriptor);

        if (drop) {
            try {
                pm.dropTable(clazz);
            } catch (Exception e) {
                e.printStackTrace();
                //
            }
        }
        return pm.createTable(clazz);
    }
}
